package top.kaoshanji.p2ch15a;

/**
 * 代码清单-p2ch15a-436页a：竞态条件示例
 * author kaoshanji
 * ^_^
 * create time 2025/2/23 15:39
 */
public class CounterThread extends Thread {

    private static int counter = 0;

    @Override
    public void run() {
        for (int i = 0; i < 1000; i++) {
            counter++;
        }
    }

    public static void main(String[] args) throws InterruptedException {

        int num = 100;
        Thread[] threads = new Thread[num];
        for (int i = 0; i < num; i++) {
            threads[i] = new CounterThread();
            threads[i].start();
        }

        for (int i = 0; i < num; i++) {
            threads[i].join();
        }

        System.out.println(counter);

    }

}
